#include "StdAfx.h"
GameManager::GameManager(void)
{
	SceneDesc sdesc;
	sdesc.AmbientLight=Vec3(.2,.2,.2);
	MainScene=sptr<Scene>(new Scene(sdesc));
	bPaused=bInit=false;
}
GameManager::~GameManager(void)
{
}
void GameManager::Init()
{
	CameraDesc cdesc;
	MainScene->MainCamera=sptr<Camera>(new Camera(cdesc));
	_MAIN_CAMERA->FocusDistance=50;
	_MAIN_CAMERA->bFocus=true;

	float clothSize=50;
	NxClothDesc clothDesc; 
	clothDesc.thickness = 0.1;
	clothDesc.density = 0.01f;
	clothDesc.attachmentResponseCoefficient = 0;
	clothDesc.bendingStiffness = 0.05;
	clothDesc.flags |= NX_CLF_BENDING | NX_CLF_BENDING_ORTHO;
	clothDesc.flags |= NX_CLF_COLLISION_TWOWAY;
	clothDesc.windAcceleration = NxVec3(0, -100, 0);

	PawnDesc pdesc;
	ModelComponentDesc mcdesc;
	pdesc.Name="PacMan";
	mcdesc.ModelName="PacMan";
	mcdesc.Pos=Vec3(0,0,0);
	mcdesc.Scale=Vec3(1,1,1);
	mcdesc.bRefracts=true;
	mcdesc.DiffuseFactor=0;

	pdesc.Pos=Vec3(0,11,0);
	pdesc.bHasCloth=true;
	pdesc.ClothDesc=clothDesc;
	pdesc.ClothDimensions=Vec3(30,30,3);
	pdesc.ClothDesc.globalPose.t = NxVec3(15,10,0);
	pdesc.ClothMaterialName="PacmanCapeMat";
	pdesc.ModelDescs.push_back(mcdesc);
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));

	Vec3 ghostPos=Vec3(100,20,100);
	pdesc.Name="Ghostie";
	pdesc.bHasCloth=true;
	pdesc.bVisible=false;
	pdesc.bKinematic=true;
	pdesc.bAI=true;
	pdesc.ClothDimensions=Vec3(50,50,2);
	pdesc.ClothDesc.globalPose.t=Vec3(ghostPos.x+clothSize/2,ghostPos.y+9.9,ghostPos.z+clothSize/2);
	pdesc.ClothMaterialName="TrollGhostMat";
	pdesc.Pos=ghostPos;
	pdesc.ClothDimensions=Vec3(clothSize,clothSize,2);
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));

	pdesc.Name="Pillar";
	mcdesc.ModelName="Column";
	mcdesc.bReflects=mcdesc.bRefracts=false;
	pdesc.bHasCloth=false;
	pdesc.bVisible=true;
	pdesc.bKinematic=false;
	pdesc.bAI=false;

	pdesc.ModelDescs.clear();
	pdesc.ModelDescs.push_back(mcdesc);
	pdesc.Pos=Vec3(50,0,0);
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));
	pdesc.Pos=Vec3(0,0,50);
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));
	pdesc.Pos=Vec3(0,0,-50);
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));

	pdesc.Pos=Vec3(-50,0,0);
	pdesc.ClothMaterialName="BlueMarbleMat";
	MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));

	mcdesc.ModelName="Pellet";
	pdesc.bHasCloth=false;
	pdesc.ModelDescs.clear();
	pdesc.ModelDescs.push_back(mcdesc);

	int nPellets=10;
	float pelletDistance=80;
	for(int i=0;i<nPellets;i++)
	{
		pdesc.Name="Pellet";//<<string::intToAlpha(i);
		pdesc.Pos=Vec3(std::sin(i*2*PI/(float)nPellets), 0, std::cos(i*2*PI/(float)nPellets))*pelletDistance;
		MainScene->Actors.push_back(sptr<Actor>(new Pawn(pdesc)));
	}

	LightDesc ldesc;
	ldesc.Pos=Vec3(0,100,300);
	ldesc.Dir=Vec3(0,-3,-10);
	ldesc.Color=Vec3(.8,.7,.2);
	ldesc.Name="Light1";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));

	ldesc.Pos=Vec3(140,10,0);
	ldesc.Color=Vec3(.2,.7,.8);
	ldesc.bOrbit=true;
	ldesc.Focus=Vec3(0,0,0);
	ldesc.OrbitVel=Vec3(0,10,0);
	ldesc.Name="Light2";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));

	ldesc.OrbitVel=Vec3(0,20,0);
	ldesc.Pos.y=20;
	ldesc.Color=Vec3(.7, .2, .8);
	ldesc.Name="Light3";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));
	
	ldesc.OrbitVel=Vec3(0,30,0);
	ldesc.Pos.y=30;
	ldesc.Color=Vec3(.8, .1, .1);
	ldesc.Name="Light4";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));

	ldesc.OrbitVel=Vec3(0,40,0);
	ldesc.Pos.y=40;
	ldesc.Color=Vec3(.1, .7, .1);
	ldesc.Name="Light5";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));

	ldesc.OrbitVel=Vec3(0,0,0);
	ldesc.Pos=Vec3(50, 100, -300);
	ldesc.Color=Vec3(.1, .1, .6);
	ldesc.Name="Light6";
	MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));

	ldesc.Pos=Vec3(0,30,0);
	ldesc.Color=Vec3(.4,.4,.4);
	ldesc.Name="Light10";
	//MainScene->Actors.push_back(sptr<Actor>(new Light(ldesc)));
	bInit=true;
}
void GameManager::Update(double t)
{
	if(!bPaused)
	{
		if(!bInit)
			Init();
		_MAIN_CAMERA->Focus=_PLAYER->Pos;
		Pawn* ghostie=_PAWN(_ACTOR("Ghostie"));
		if(ghostie)
			ghostie->AI();
		globals.Input.hasMoved=false;
		for(unsigned int i=0;i<MainScene->Actors.size();i++)
		{
			sptr<Actor> actor=MainScene->Actors[i];
			actor->Update(t);
		}
		_MAIN_SCENE->simulate(t);
		_MAIN_SCENE->flushStream();
		_MAIN_SCENE->fetchResults(NX_RIGID_BODY_FINISHED, true);
	}
}